package com.rbcs.base.currency.webapp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.rbcs.base.currency.service.CurrencyManager;
import com.rbcs.base.model.Currency;
import com.systop.common.modules.security.user.UserUtil;
import com.systop.common.modules.security.user.model.User;
import com.systop.core.dao.support.Page;
import com.systop.core.util.PageUtil;
import com.systop.core.webapp.struts2.action.DefaultCrudAction;

/**
 * 外币币种管理Action
 * @author ShangHua
 */
@SuppressWarnings({"serial", "unchecked", "rawtypes"})
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class CurrencyAction extends DefaultCrudAction<Currency, CurrencyManager> {
	
	/**
	 * 查看外币币种信息
	 */
	@Override
	public String index() {
		Page page = PageUtil.getPage(getPageNo(), getPageSize());
		StringBuffer hql = new StringBuffer("from Currency c where 1 = 1");
		List args = new ArrayList();
		if (StringUtils.isNotBlank(getModel().getName())) {
			hql.append(" and c.name like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(getModel().getName()));
		}
		page = getManager().pageQuery(page, hql.toString(), args.toArray());
		restorePageData(page);
		return INDEX;
	}
	
	/**
	 * 保存外币币种信息
	 */
	@Override
	public String save() {
		User user = UserUtil.getPrincipal(getRequest());
		if (user != null) {
			getModel().setCreator(user);
		} else {
			this.addActionMessage("当前登录用户信息不存在，请您重新登录系统！");
			return INPUT;
		}
    try {
  		getModel().setCreateTime(new Date());
  		getManager().save(getModel());
      return SUCCESS;
    } catch (Exception e) {
      addActionError(e.getMessage());
      return INPUT;
    }
	}
}
